package com.hanggold.springboot.lesson4.select;

import com.hanggold.springboot.lesson4.annotation.EnableServerDefinition;
import com.hanggold.springboot.lesson4.enumo.ServerType;
import com.hanggold.springboot.lesson4.service.FtpServer;
import com.hanggold.springboot.lesson4.service.HttpServer;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.Ordered;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

public class MySelectorImportRegistrar implements ImportBeanDefinitionRegistrar{


    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        Map<String, Object> map = importingClassMetadata.getAnnotationAttributes(EnableServerDefinition.class.getName());

        ServerType serverType = (ServerType) map.get("type");
        String className  = FtpServer.class.getName();
        if (serverType == ServerType.HTTP) {
            className = HttpServer.class.getName();
        }
        BeanDefinitionBuilder builder  = BeanDefinitionBuilder.genericBeanDefinition(className);
//        registry.registerBeanDefinition();
        registry.registerBeanDefinition("http",builder.getBeanDefinition());

//        BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(),registry);


    }
}
